<template>
  <t-dialog
    header="评审判定"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="60%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form ref="form" :colon="true" resetType="initial" :data="formData" labelWidth="150px" :rules="rules">

        <t-form-item label="科普站名称">
          <t-link @click="dialog.header=projectName;dialog.open_detail=true" hover="color" theme="primary">{{projectName}}</t-link>
        </t-form-item>
        <t-form-item label="评审类型" name="reviewType">
<!--          <t-select style="width: 390px" disabled placeholder="评审业务类型" clearable filterable-->
<!--                    :options="dict.type.review_type"-->
<!--                    v-model="formData.reviewType">-->
<!--          </t-select>-->
          {{ formData.reviewType | filterByDict(dict.type.kpzh_review_type) }}
        </t-form-item>
        <t-form-item v-if="reviewType==='1'" label="评审平均分">
          {{ formData.sumScore }}
        </t-form-item>
        <t-form-item label="是否通过" name="projectStatus">
          <t-radio-group v-model="formData.projectStatus">
            <t-radio value="3" v-if="reviewType === '3'"> 优秀</t-radio>
            <t-radio value="1"> 是</t-radio>
            <t-radio value="2" v-if="reviewType!=='1'">需要复议</t-radio>
            <t-radio value="0"> 否</t-radio>
          </t-radio-group>
        </t-form-item>
        <!-- 通过 第一次立项评审 一般科技项目 -->
        <t-form-item v-if="formData.projectStatus === '1' && formData.reviewType === '4' && [1660720545908].includes(formData.projectCategory)" label="是否需要二次评审" name="secondReviewType">
          <t-radio-group v-model="formData.secondReviewType">
            <t-radio value="1"> 是</t-radio>
            <t-radio value="0"> 否</t-radio>
          </t-radio-group>
        </t-form-item>
        <template v-else-if="formData.projectStatus === '1'  && reviewType==='5' ">
          <t-form-item v-if="isShow" label="项目编号" name="projectNum">
            <t-input clearable style="width: 390px" placeholder="请输入项目编号" v-model="formData.projectNum"></t-input>
          </t-form-item>
          <t-form-item v-if="site_tag === 'jtt'" label="计划类别" name="projectCategory">
            <t-cascader @change="onProjectCategoryChange" clearable filterable style="width: 390px"
                        v-model="formData.projectCategory" :options="categoryOptions"/>
          </t-form-item>
          <t-form-item v-if="isShow" label="学科分类" name="subjectClassification">
            <t-select clearable style="width: 390px" v-model="formData.subjectClassification"
                      :options="dict.type.subject_classification">
            </t-select>
          </t-form-item>
        </template>

        <t-form-item label="备注" name="content">
          <t-textarea
            clearable :autosize="{minRows: 5}"
            v-model="formData.content"
            placeholder="请输入内容"
          />
        </t-form-item>
      </t-form>
      <project-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail"
                             v-if="dialog.open_project_detail"></project-detail-dialog>
      <item-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_item_detail"
                             v-if="dialog.open_item_detail"></item-detail-dialog>
    
        <base-detail-dialog  :header="dialog.header" :obj-id="dialog.objId" :visible.sync="dialog.open_detail" v-if="dialog.open_detail"></base-detail-dialog>
        </div>
  </t-dialog>
</template>

<script>
import ProjectDetailDialog from "@/pages/kjxm/pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "@/pages/kjxm/pm/components/item-detail-dialog.vue";
import BaseDetailDialog from "@/pages/kpzh/science-base/components/base-detail-dialog.vue";

export default {
  name: "project-approval",
  dicts: ['sys_normal_disable', 'subject_classification', 'kpzh_review_type', 'project_type'],
  components: {
    ProjectDetailDialog,
    ItemDetailDialog,
    BaseDetailDialog
  },
  props: ["objId", "reviewType", "projectName", "projectId"],
  data() {
    return {
      isShow: true,
      site_tag: import.meta.env.VITE_SITE_TAG,
      dialog: {
        header: '',
        open_project_detail: false,
        objId: null,
        open_item_detail: false,
        open_detail: false
      },
      visible: true,
      loading: false,
      projectStatusOptions: [],
      projectCategoryData: [],
      categoryOptions: [],
      formData: {
        id: this.objId,
        projectStatus: '1',
        secondReviewType: '1',
        reviewType: this.reviewType,
        content: '',
        businessId: this.projectId,
        projectNum: '',
        projectCategory: '',
        subjectClassification: '',
        projectType: '',
        version: '',
      },
      rules: {
        projectCategory: [
          {required: true, message: "计划类别不能为空"}
        ],
        subjectClassification: [
          {required: true, message: "学科分类不能为空"}
        ],
        projectType: [
          {required: true, message: "项目类型不能为空"}
        ],
        projectNum: [
          {required: true, message: "项目编号不能为空"}
        ],
        content: [
          {required: true, message: "评审意见不能为空"}
        ],
        score: [
          {required: true, validator: (val) => val >= 0, message: "大于等于0", type: "error"}
        ],
        projectStatus: [
          {required: true, message: "状态不能为空"}
        ]
      }
    }
  },
  async created() {
    // await this.getPlanCategoryTreeSelect();
    this.getReviewResult();
    this.show();
  },
  methods: {
    show() {
      if (this.formData.reviewType === '4') {
        this.isShow = true;
        return
      }
      if (this.formData.reviewType === '5') {
        this.isShow =  false;
        return
      }
      if (![1662099020236, 1660720633654].includes(this.formData.projectCategory)) {
        this.isShow =  true;
        return
      }
      this.isShow =  false;
    },
    onProjectCategoryChange(val){
      this.$refs.form.reset();
      this.formData.projectCategory = val;
    },
    getPlanCategoryTreeSelect() {
      return this.$api.kpzh.project.planCategoryList().then(
        (res) => {
          this.projectCategoryData = this.handleTree(res.data, "id");
          const projectCategoryData = res.data.filter((val) => {
            if(!val.parentId){return true}
          })
          this.categoryOptions = projectCategoryData.map((val) => ({label: val.name, value: val.id}))
        }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getReviewResult() {
      // 获取评审信息
      this.$api.kpzh.review.projectReviewTeamGet(this.objId).then(response => {
        if (response.data) {
          Object.assign(this.formData, response.data)
          this.formData.projectNum = response.data?.project?.projectNum;
          this.formData.subjectClassification = response.data?.project?.subjectClassification;
          this.formData.projectType = response.data?.project?.projectType;
          this.formData.reviewType = response.data?.reviewType;
          this.formData.projectCategory = response.data?.project?.projectCategory;
          this.formData.projectStatus = response.data.projectStatus || '1';
          this.formData.secondReviewType = response.data?.project?.secondReviewType || '0'
          this.formData.version = response.data?.project?.version;
          this.dialog.objId = response.data?.project?.id;
        }
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      if (this.formData.projectStatus !== '1') {
        this.formData.secondReviewType = null;
      }
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          this.$api.kpzh.review.teamProjectReviewUpdate(this.formData).then(reponse => {
            this.$message.success("提交成功");
            this.$emit("reload");
            this.$emit("update:visible", false);
            this.loading = false;
          }).catch((e) => {
            this.loading = false;
            this.$message.error(e.toString());
          });
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
